package com.cqsw.filter;


import com.cqsw.utils.ApplicationContextAwreaUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Classname LoginFilter
 * @Description TODO
 * @Date 2022/6/13 14:39
 * @Created by Smith.Liu
 */

public class LoginFilter extends UsernamePasswordAuthenticationFilter {

    public static final String SFROM_VAIIDATE_KEY = "validateCode";

    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    public LoginFilter(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    private String validateCodeParameter = SFROM_VAIIDATE_KEY;

    public LoginFilter() {

    }

    public String getValidateCodeParameter() {
        return validateCodeParameter;
    }

    public void setValidateCodeParameter(String validateCodeParameter) {
        this.validateCodeParameter = validateCodeParameter;
    }


    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        if (!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException(
                    "Authentication method not supported: " + request.getMethod());
        }
        if (request.getContentType().equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE)) {
            try {
                Map<String, String> userInfo = new ObjectMapper().readValue(request.getInputStream(), Map.class);
                String username = userInfo.get(getUsernameParameter());
                String password = userInfo.get(getPasswordParameter());
                String validateCode = userInfo.get(getValidateCodeParameter());

                StringRedisTemplate stringRedisTemplate = (StringRedisTemplate) ApplicationContextAwreaUtils.getBean("stringRedisTemplate");
                String redisValiCode = stringRedisTemplate.opsForValue().get(getValidateCodeParameter());

                if (!ObjectUtils.isEmpty(validateCode) && !ObjectUtils.isEmpty(redisValiCode) && validateCode.equalsIgnoreCase(redisValiCode)) {
                    UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
                    setDetails(request, authRequest);
                    return this.getAuthenticationManager().authenticate(authRequest);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        throw new UsernameNotFoundException("验证码不匹配");
    }
}
